from flask.views import View
from flask import request, jsonify
from datetime import datetime
import os
import base64


class Uedit(View):
    methods = ['POST', 'GET', 'OPTIONS']

    def dispatch_request(self):
        result = {}
        action = request.args.get('action')
        # 解析JSON格式的配置文件

        if action == 'config':
            # 初始化时，返回配置文件给客户端
            result = {"imageActionName": "uploadimage",
                      "snapscreenActionName": "uploadimage",
                      "fileActionName": "uploadfile",
                      "videoActionName": "uploadvideo",
                      "scrawlActionName": "uploadscrawl",
                      "imageFieldName": "upfile",
                      "fileFieldName": "upfile",
                      "videoFieldName": "upfile",
                      "scrawlFieldName": "upfile",
                      "snapscreenFieldName": "upfile",
                      "imageMaxSize": 2048000,
                      "fileMaxSize": 51200000,
                      "videoMaxSize": 51200000,
                      "scrawlMaxSize": 2048000,
                      "imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif"],

                      "fileAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp", ".html",
                                         ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",
                                         ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid",
                                         ".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso", ".csv",
                                         ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md",
                                         ".xml"],
                      "videoAllowFiles": [".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg", ".ogg",
                                          ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid"
                                          ],
                      "imageCompressEnable": True,
                      "imageCompressBorder": 1600,
                      "imageInsertAlign": "none",
                      "snapscreenInsertAlign": "none",
                      "scrawlInsertAlign": "none",
                      "imageUrlPrefix": "",
                      "snapscreenUrlPrefix": "",
                      "fileUrlPrefix": "",
                      "videoUrlPrefix": "",
                      "scrawlUrlPrefix": "",
                      "imagePathFormat": "",
                      "filePathFormat": "",
                      "scrawlPathFormat": "",
                      "videoPathFormat": ""
                      }
        if action == 'uploadimage':
            result = upload_image()
        if action == 'uploadfile':
            result = upload_file()
        if action == 'uploadvideo':
            result = upload_video()
        if action == 'uploadscrawl':
            result = upload_scrawl()
        return jsonify(result)


# 涂鸦处理
def upload_scrawl():
    data = base64.b64decode(request.form.get('upfile'))
    name = datetime.now().strftime('%Y%m%d%H%M%S')
    filename = name + '.png'
    with open("static/upload/scrawl/{}".format(filename), 'wb') as scr:
        scr.write(data)
       
   # open("static/upload/scrawl/{}/".format(filename), 'wb').write(data)
    return {"state": "SUCCESS",
            "url": "/upload/scrawl/{}/".format(filename),
            "title": filename,
            "original": filename,
            }


# 视屏处理
def upload_video():
    file_storage = request.files['upfile']
    name, extension = os.path.splitext(file_storage.filename)
    name = datetime.now().strftime('%Y%m%d%H%M%S')
    filename = name + extension
    file_path = os.path.join(r"static/upload/video/", filename)
    file_storage.save(file_path)
    return {"state": "SUCCESS",
            "url": file_path.replace('static/', '/'),
            "title": file_storage.filename,
            "original": file_storage.filename,
            }


# 上传文件处理
def upload_file():
    file_storage = request.files['upfile']
    name, extension = os.path.splitext(file_storage.filename)
    name = datetime.now().strftime('%Y%m%d%H%M%S')
    filename = name + extension
    file_path = os.path.join(r"static/upload/file/", filename)
    file_storage.save(file_path)
    return {"state": "SUCCESS",
            "url": file_path.replace('static/', '/'),
            "title": file_storage.filename,
            "original": file_storage.filename
            }


# 上传照片包含截屏处理
def upload_image():
    file_storage = request.files['upfile']
    name, extension = os.path.splitext(file_storage.filename)
    name = datetime.now().strftime('%Y%m%d%H%M%S')
    filename = name + extension
    file_path = os.path.join(r"static/upload/image/", filename)
    file_storage.save(file_path)
    return {"state": "SUCCESS",
            "url": file_path.replace('static/', '/'),
            "title": file_storage.filename,
            "original": file_storage.filename,
            "type": extension,
            "size": ""}
